/**
 * Task.java Create on 2012-4-10     
*      
* Copyright (c) 2012-4-10 by yiming     
*      
* @author <a href="alenzhangym@hotmail.com">Yiming</a>     
* @version 1.0 
 */
package com.yimi.taskmanager;

import com.yimi.common.Message;
import com.yimi.service.Entity;

import java.util.Date;
import java.util.Vector;

/**
 * TODO 功能描述    
 *      
 *  
 *      
 * @author Yiming
 * @version 1.0 
 */
public abstract class Task implements Runnable {
	   // private static Logger logger = Logger.getLogger(Task.class);
    /* 产生时间 */
    private Date generateTime = null;
    /* 提交执行时间 */
    private Date submitTime = null;
    /* 开始执行时间 */
    private Date beginExceuteTime = null;
    /* 执行完成时间 */
    private Date finishTime = null;
    protected Vector<TaskListener> listners;
    private long taskId;
    protected String taskCommand;
  

    private boolean close;
    private boolean needExceuteImmediate;
    public Task(String command) {
        this.taskCommand = command;
        this.listners = new Vector<TaskListener>();
        this.generateTime = new Date();
        close = false;
    }
    public String getTaskCommand() {
        return taskCommand;
    }

    public void setTaskCommand(String taskCommand) {
        this.taskCommand = taskCommand;
    }
    /**
    * 是否需要立即执行
    * 
    * @return
    */
    protected  boolean needExecuteImmediate(){
        return needExceuteImmediate;
    }
    
    public  void setNeedExecuteImmediate(boolean done){
        this.needExceuteImmediate = done;
    }
    /**
    * 任务信息
    * 
    * @return String
    */
    public abstract String info();
    public abstract void stop();
    
    public Date getGenerateTime() {
        return generateTime;
    }

    public Date getBeginExceuteTime() {
        return beginExceuteTime;
    }

    public void setBeginExceuteTime(Date beginExceuteTime) {
        this.beginExceuteTime = beginExceuteTime;
    }

    public Date getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(Date finishTime) {
        this.finishTime = finishTime;
    }

    public Date getSubmitTime() {
        return submitTime;
    }

    public void setSubmitTime(Date submitTime) {
        this.submitTime = submitTime;
    }

    public long getTaskId() {
        return taskId;
    }

    public void setTaskId(long taskId) {
        this.taskId = taskId;
    }
    
    public synchronized void registerTaskListener(TaskListener listener) {
        listners.add(listener);
    }

    public synchronized void unRegisterDownloadImageListener(TaskListener listner) {
        listners.remove(listner);
    }
    /**
     * @param message
     */
    protected synchronized void addMessage(Message message) {
        for (int i = 0; i < listners.size(); i++) {
            TaskListener listener = listners.get(i);
            listener.addMessage(message);
        }
    }
    
    protected void addFailedMessage(int event,int type,Entity entity) {
        {
            final Message message = new Message(event);
            message.setType(type);
            message.setResult(entity);
            addMessage(message);
        }
    }
}
